

package edu.byu.intex2.data.dao;

import java.sql.*;
import java.util.List;
import java.util.LinkedList;
import edu.byu.intex2.data.*;
import edu.byu.intex2.data.bo.*;

/**
 * A singleton object that CRUD's NewCar objects.
 *
 * @author Jason Whiting <jason_whiting@byu.edu>
 * @version 2009-02-05
 */
public class NewCarDAO  {

  /////////////////////////////////////////////
  ///   Singleton code

  private static NewCarDAO instance = null;

  /** Creates a new instance of NewCarDAO */
  private NewCarDAO() {
  }//constructor

  /** Retrieves the single instance of this class */
  public static synchronized NewCarDAO getInstance() {
    //singleton code
    //return the instance
  }//getInstance


  ////////////////////////////////////////////
  ///   CREATE methods

  /** Creates a new newcar in the database */
  public NewCar create(String id) throws DataException {
    //create a new blank NewCar object (pass in GUID)
	//set the alreadyInDB variable to false
	//put to cache and return the new object
  }//create



  ////////////////////////////////////////////
  ///   READ methods

  /** Reads an existing newcar from the database */
  public NewCar read(String id) throws DataException {
    //check to see if the object is in cache, if it is just return it
	//establish a connection from the pool, then pass that connection along with the string id to the next method
	//catch any exceptions you may have
	//return the NewCar
	//RELEASE THE CONNECTION!!
  }

  /** Internal method to read an existing newcar from the database */
  synchronized NewCar read(String id, Connection conn) throws Exception {
    //check the cache just like above just to make sure
	//use a prepared statement to prevent against sql injection
	//run the query and get the result set. have the next method process the result set
	//catch any exceptions you may have
	//return the NewCar
  }//read


  /** Internal method to create a newcar object from a record */
  synchronized NewCar readRecord(ResultSet rs, Connection conn) throws Exception {
    //again, check the cache, but check it with the id from the record set that was passed
	//create a new NewCar object. don't forget to put it to cache, set it's alreadyInDB param to true and dirty to false
	//return the NewCar
  }//readRecord



  /////////////////////////////////////////////
  ///   UPDATE methods

  /** Saves an existing newcar in the database */
  public void save(NewCar newcar) throws DataException {
    //get a connection from the pool, save all of the NewCars first using the other save method and then commit them in the database
	//rollback if there were any errors saving, catch any other errors you might have
	//release the connection!!
  }//update

  /** Internal method to update a newcar in the database */
  void save(NewCar newcar, Connection conn) throws Exception {
     //check cache first, if returns nothing, put the NewCar object to cache
	//decide whether to update or insert into the database

 }//save

  /** Saves an existing newcar to the database */
  private void update(NewCar newcar, Connection conn) throws Exception {
    //use a prepared statement to prevent sql injection
	//run the query and set dirty to false
	//make sure to close the prepared statement
  }

  /** Inserts a new newcar into the database */
  private void insert(NewCar newcar, Connection conn) throws Exception {
    //use a perpared statement
	//set the alreadyInDB variable to true
	//dirty to false
	//close the prepared statement
  }





  /////////////////////////////////////////////////
  ///   DELETE methods

  /** We do not support deleting of business objects in this application */
  public void delete(NewCar newcar) throws DataException {
    throw new RuntimeException("Nice try. The delete function is not supported in this application.");
  }



  ////////////////////////////////////////////////
  ///   SEARCH methods

  /** Retrieves all newcars from the database */
  public List<NewCar> getAll() throws DataException {
    //establish a connection from the pool, use prepared statement, use the search method below to run the statement, catch any errors
	//release the connection
  }

  /** Internal method to search by certain fields */
  private List<NewCar> search(PreparedStatement stmt, Connection conn) throws Exception {
    //instantiate a list of NewCars to add recordsets to. this will make going through the list easier
	//finally, close the statement and return the list
  }



}//class
